package com.example.rjb_shoping.auth;

import com.example.rjb_shoping.util.RsaUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * 公钥、密钥属性对象
 */
@Data
//@ConfigurationProperties(prefix = "rsa.key")
@Component
public class RsaKeyProperties {
    @Value("${rsa.key.pubKeyFile}")
    private String pubKeyFile;
    @Value("${rsa.key.priKeyFile}")
    private String priKeyFile;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    /**
     * 系统启动的时候触发
     * @throws Exception
     */
    @PostConstruct
    public void createRsaKey() throws Exception {
//        System.out.println("pubKeyFile ==" + pubKeyFile);
//        System.out.println("priKeyFile ==" + priKeyFile);
        publicKey = RsaUtils.getPublicKey(pubKeyFile.getBytes("UTF-8"));
        privateKey = RsaUtils.getPrivateKey(priKeyFile.getBytes("UTF-8"));
    }
}
